package fr.playsoft.psas;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import fr.playsoft.psas.maps.MyItemizedOverlay;
import fr.playsoft.psas.maps.Road;
import fr.playsoft.psas.maps.RoadProvider;
import fr.playsoft.psas.maps.RouteOverlay;

public class MapRoute extends MapActivity {

	public static final String[] SPINNER_TEXT = { "Paris - France",
			"London - UK", "Lille - France", "Cannes - France" };

	public static final String[] MAP_SNIPPET_TEXT = {
			"Paris - Location rom MobisanT project",
			"London - where my friends go to study abroad",
			"Lille - Location from Happy Lille project",
			"Cannes - Location from Festival de Cannes project" };

	public static final double GEO_PARIS_LAT = 48.85737;
	public static final double GEO_PARIS_LNG = 2.34923;
	public static final double GEO_PARIS_LAT_DEST = 48.85737;
	public static final double GEO_PARIS_LNG_DEST = 2.35923;

	public static final double GEO_LONDON_LAT = 51.50074;
	public static final double GEO_LONDON_LNG = -0.1244;

	public static final double GEO_LILLE_LAT = 50.62967;
	public static final double GEO_LILLE_LNG = 3.0574;

	public static final double GEO_CANNES_LAT = 43.55283;
	public static final double GEO_CANNES_LNG = 7.0174;

	private Spinner mSpinner;
	private MapView mapView;
	private MapController mc;

	private int mSelectedIndex;
	private GeoPoint[] mGeoPoint;

	LinearLayout linearLayout;
	private Road mRoad;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maproute);

		mSelectedIndex = 0;
		mGeoPoint = new GeoPoint[SPINNER_TEXT.length];

		mGeoPoint[0] = new GeoPoint((int) (GEO_PARIS_LAT * 1E6),
				(int) (GEO_PARIS_LNG * 1E6));
		mGeoPoint[1] = new GeoPoint((int) (GEO_LONDON_LAT * 1E6),
				(int) (GEO_LONDON_LNG * 1E6));
		mGeoPoint[2] = new GeoPoint((int) (GEO_LILLE_LAT * 1E6),
				(int) (GEO_LILLE_LNG * 1E6));
		mGeoPoint[3] = new GeoPoint((int) (GEO_CANNES_LAT * 1E6),
				(int) (GEO_CANNES_LNG * 1E6));

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
		new Thread() {
			@Override
			public void run() {
				double fromLat = GEO_PARIS_LAT, fromLon = GEO_PARIS_LNG, 
						toLat = GEO_PARIS_LAT_DEST, toLon = GEO_PARIS_LNG_DEST;
				String url = RoadProvider
						.getUrl(fromLat, fromLon, toLat, toLon);
				InputStream is = getConnection(url);
				mRoad = RoadProvider.getRoute(is);
				mHandler.sendEmptyMessage(0);
			}
		}.start();
	}
	
	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			TextView textView = (TextView) findViewById(R.id.description);
			textView.setText(mRoad.mName + " " + mRoad.mDescription);
			RouteOverlay mapOverlay = new RouteOverlay(mRoad, mapView);
			List<Overlay> listOfOverlays = mapView.getOverlays();
			listOfOverlays.clear();
			listOfOverlays.add(mapOverlay);
			/*
			Drawable drawable = getResources().getDrawable(R.drawable.marker);
			MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
			List<Overlay> mapOverlays = mapView.getOverlays();
			
			GeoPoint dest = new GeoPoint((int) (GEO_PARIS_LAT_DEST * 1E6),
					(int) (GEO_PARIS_LNG_DEST * 1E6));
			OverlayItem overlayItem = new OverlayItem(mGeoPoint[0], SPINNER_TEXT[0], MAP_SNIPPET_TEXT[0]);
			itemizedOverlay.addOverlay(overlayItem);
			
			overlayItem = new OverlayItem(dest, SPINNER_TEXT[0], MAP_SNIPPET_TEXT[0]);
			itemizedOverlay.addOverlay(overlayItem);
			
			mapOverlays.add(itemizedOverlay);*/
			
			mc = mapView.getController();
			mc.animateTo(mGeoPoint[0]);
			mc.setZoom(16);
			
			mapView.invalidate();
		};
	};

	private InputStream getConnection(String url) {
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
